package org.wit.ecommerce.action.admin;

import java.sql.Timestamp;
import java.util.Calendar;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.wit.ecommerce.domain.PartnerUser;
import org.wit.ecommerce.domain.User;
import org.wit.ecommerce.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
@Namespace("/admin/user")
public class UserAction extends ActionSupport {
	private String email;
	private String password;
	private String verification;
	private String tip;
	private PartnerUser partner;
    @Autowired
	private IUserService userService;
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService ){
		this.userService=userService;
	}
	@Action("test")
	public String test(){
		return SUCCESS;
	}
	@Action("register")
	public String register()throws Exception{
	   return SUCCESS;
	}
	@Action("login")
	public String login() throws Exception{
		return SUCCESS;
	}
	@Action("welcome")
	public String registeruser()throws Exception{
		partner.setCreateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));
	    userService.createUser(partner);
		setTip("register success"+" "+partner.getEmail());
		return SUCCESS;
	}
	@Action(value="main", results={@Result(name="success", location="/WEB-INF/jsp/admin/main.jsp")})
	public String execute(){
		User user = userService.login(getEmail(), getPassword());
		if(user == null) {
			return LOGIN;
		}else{ 
			setTip("welcome"+" "+getEmail());
			return SUCCESS;
		}
	}
	public void setPartner(PartnerUser partner) {
		this.partner = partner;
	}
	public PartnerUser getPartner() {
		return partner;
	}
	public String getVerification() {
		return verification;
	}
	public void setVerification(String verification) {
		this.verification = verification;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setTip(String tip){
		this.tip=tip;
	}
	public String getTip(){
		return tip;
	}
}
